<?php
namespace app\common\model;

use think\model\concern\SoftDelete;

class Goods extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    public function getTypeAttr($val)
    {
        $type = [1 => '普通商品', 2 => '佣金商品', 3 => '红包商品', 4 => '积分商品'];
        return $type[$val];
    }

    public function getCateIdAttr($val)
    {
        if($val == 0) return '';
        return GoodsCate::getVal(['id' => $val], 'name');
    }

    // public function getShopIdAttr($val)
    // {
    //     if($val == 0) return '';
    //     return Shop::getVal(['id' => $val], 'name');
    // }

    public function getSmallImgAttr($val)
    {
        return getUrl() . $val;
    }

    /**
     * 确认订单
     */
    public static function orderConfirm($user_id, $goodsId, $price, $number, $sku)
    {
        $userInfo  = User::getFind(['id' => $user_id]);
        $goodsInfo = self::where('id', $goodsId)->find();

        if(!$goodsInfo) return false;

        $shopInfo = [
            'shop_id'    => $goodsInfo['shop_id'],
            'shop_name'  => Shop::getVal(['id' => $goodsInfo['shop_id']], 'name'),
            'price'      => $price,
        ];

        $goodsInfos = [
            "id"         => $goodsInfo['id'],
            "name"       => $goodsInfo['name'],
            "small_img"  => $goodsInfo['small_img'],
            "type"       => $goodsInfo['type'],
            "price"      => $goodsInfo['price'],
            "number"     => $number,
            "sku"        => $sku,
        ];

        switch($goodsInfo['type']){
            case '佣金商品':
                $shopInfo['user_brokerage']    = $userInfo['money_brokerage'];
                $shopInfo['rebate_brokerage']  = $goodsInfo['price'] * $goodsInfo['brokerage_ratio'];
                $goodsInfo['rebate_brokerage'] = $goodsInfo['price'] * $goodsInfo['brokerage_ratio'];
                break;
            case '红包商品':
                $shopInfo['user_money_red']    = $userInfo['money_red'];
                $shopInfo['rebate_money_red']  = $goodsInfo['money_red'];
                $goodsInfo['rebate_money_red'] = $goodsInfo['money_red'];
                break;
            case '积分商品':
                $shopInfo['rebate_point']  = $goodsInfo['price'] * $goodsInfo['point_ratio'];
                $goodsInfo['rebate_point'] = $goodsInfo['price'] * $goodsInfo['point_ratio'];
                break;
        }

        $shopInfo['goods_list'][] = $goodsInfos;

        return $shopInfo;
    }
}